﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.ViewModels;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement;
using EDB.SmoothStreaming.Models;

namespace EDB.SmoothStreaming.Settings {
    public class VideoPartSettings {
        public const int ThumbnailWidthDefaultDefault = 128;
        public const int ThumbnailHeightDefaultDefault = 72;
        public const int ThumbnailCountDefaultDefault = 8;
        public const string VideoStreamsDefaultDefault = "640,360,1200";
        public const int PlayerWidthDefaultDefault = 640;
        public const int PlayerHeightDefaultDefault = 400;
        public const ThumbnailFormat ThumbnailFormatDefaultDefault = ThumbnailFormat.Jpg;

        private int? _thumbnailWidthDefault;
        private int? _thumbnailHeightDefault;
        private int? _thumbnailCountDefault;
        private string _videoStreamsDefault;
        private int? _playerWidthDefault;
        private int? _playerHeightDefault;
        private ThumbnailFormat? _thumbnailFormatDefault;

        public int ThumbnailWidthDefault {
            get {
                return _thumbnailWidthDefault != null
                         ? (int)_thumbnailWidthDefault
                         : ThumbnailWidthDefaultDefault;
            }
            set { _thumbnailWidthDefault = value; }
        }

        public int ThumbnailHeightDefault {
            get {
                return _thumbnailHeightDefault != null
                         ? (int)_thumbnailHeightDefault
                         : ThumbnailHeightDefaultDefault;
            }
            set { _thumbnailHeightDefault = value; }
        }

        public int ThumbnailCountDefault {
            get {
                return _thumbnailCountDefault != null
                         ? (int)_thumbnailCountDefault
                         : ThumbnailCountDefaultDefault;
            }
            set { _thumbnailCountDefault = value; }
        }

        public string VideoStreamsDefault {
            get {
                return !String.IsNullOrEmpty(_videoStreamsDefault) 
                         ? _videoStreamsDefault
                         : VideoStreamsDefaultDefault;
            }
            set { _videoStreamsDefault = value; }
        }

        public int PlayerWidthDefault {
            get {
                return _playerWidthDefault != null
                         ? (int)_playerWidthDefault
                         : PlayerWidthDefaultDefault;
            }
            set { _playerWidthDefault = value; }
        }

        public int PlayerHeightDefault {
            get {
                return _playerHeightDefault != null
                         ? (int)_playerHeightDefault
                         : PlayerHeightDefaultDefault;
            }
            set { _playerHeightDefault = value; }
        }

        public ThumbnailFormat ThumbnailFormatDefault {
            get {
                return _thumbnailFormatDefault != null
                         ? (ThumbnailFormat)_thumbnailFormatDefault
                         : ThumbnailFormatDefaultDefault;
            }
            set { _thumbnailFormatDefault = value; }
        }
    }

    public class VideoTypePartSettings {
        public int? ThumbnailWidthDefault { get; set; }
        public int? ThumbnailHeightDefault { get; set; }
        public int? ThumbnailCountDefault { get; set; }
        public string VideoStreamsDefault { get; set; }
        public int? PlayerWidthDefault { get; set; }
        public int? PlayerHeightDefault { get; set; }
        public ThumbnailFormat? ThumbnailFormatDefault { get; set; }
    }

    public class VideoSettingsHooks : ContentDefinitionEditorEventsBase {
        public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition) {
            if (definition.PartDefinition.Name != "VideoPart")
                yield break;

            var model = definition.Settings.GetModel<VideoTypePartSettings>();
            var partModel = definition.PartDefinition.Settings.GetModel<VideoPartSettings>();

            if (model.ThumbnailWidthDefault == null)
                model.ThumbnailWidthDefault = partModel.ThumbnailWidthDefault;

            if (model.ThumbnailHeightDefault == null)
                model.ThumbnailHeightDefault = partModel.ThumbnailHeightDefault;

            if (model.ThumbnailCountDefault == null)
                model.ThumbnailCountDefault = partModel.ThumbnailCountDefault;

            if (String.IsNullOrEmpty(model.VideoStreamsDefault))
                model.VideoStreamsDefault = partModel.VideoStreamsDefault;

            if (model.PlayerWidthDefault == null)
                model.PlayerWidthDefault = partModel.PlayerWidthDefault;

            if (model.PlayerHeightDefault == null)
                model.PlayerHeightDefault = partModel.PlayerHeightDefault;

            if (model.ThumbnailFormatDefault == null)
                model.ThumbnailFormatDefault = partModel.ThumbnailFormatDefault;

            yield return DefinitionTemplate(model);
        }

        public override IEnumerable<TemplateViewModel> PartEditor(ContentPartDefinition definition) {
            if (definition.Name != "VideoPart")
                yield break;

            var model = definition.Settings.GetModel<VideoPartSettings>();
            yield return DefinitionTemplate(model);
        }

        public override IEnumerable<TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel) {
            if (builder.Name != "VideoPart")
                yield break;

            var model = new VideoTypePartSettings();
            updateModel.TryUpdateModel(model, "VideoTypePartSettings", null, null);
            builder.WithSetting("VideoTypePartSettings.ThumbnailWidthDefault", model.ThumbnailWidthDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.ThumbnailHeightDefault", model.ThumbnailHeightDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.ThumbnailCountDefault", model.ThumbnailCountDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.VideoStreamsDefault", model.VideoStreamsDefault);
            builder.WithSetting("VideoTypePartSettings.PlayerWidthDefault", model.PlayerWidthDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.PlayerHeightDefault", model.PlayerHeightDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.ThumbnailFormatDefault", model.ThumbnailFormatDefault.ToString());

            yield return DefinitionTemplate(model);
        }

        public override IEnumerable<TemplateViewModel> PartEditorUpdate(ContentPartDefinitionBuilder builder, IUpdateModel updateModel) {
            if (builder.Name != "VideoPart")
                yield break;

            var model = new VideoPartSettings();
            updateModel.TryUpdateModel(model, "VideoPartSettings", null, null);
            builder.WithSetting("VideoTypePartSettings.ThumbnailWidthDefault", model.ThumbnailWidthDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.ThumbnailHeightDefault", model.ThumbnailHeightDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.ThumbnailCountDefault", model.ThumbnailCountDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.VideoStreamsDefault", model.VideoStreamsDefault);
            builder.WithSetting("VideoTypePartSettings.PlayerWidthDefault", model.PlayerWidthDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.PlayerHeightDefault", model.PlayerHeightDefault.ToString());
            builder.WithSetting("VideoTypePartSettings.ThumbnailFormatDefault", model.ThumbnailFormatDefault.ToString());

            yield return DefinitionTemplate(model);
        }
    }
}